/**
* 
*/
package com.naiterui.ehp.bs.ds.common.utils;

import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bp.utils.notice.SendMessageUtil;
import com.naiterui.ehp.bp.utils.thread.ThreadPoolUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SMSUtil {

    private static String url = ConfigUtil.getString("ehp.sms.singlesned.url");
    private static SendMessageUtil sendMessageUtil = new SendMessageUtil(url);

    public enum Template {
        /** 登录验证码 */
        USER_LOGIN_SMS_CDOE (ConfigUtil.getString("ehp.sms.template.logincode"));

        private String templateId;

        Template(String templateId) {
            this.templateId = templateId;
        }

        public String getTemplateId() {
            return this.templateId;
        }

        public void setTemplateId(String templateId) {
            this.templateId = templateId;
        }
    }

    /**
     * 根据模板发送短信
     */
    public static void sendSMS(String phone, Template tmpl, Object... args) {
        String templateId = tmpl.getTemplateId();
        log.info("[smsSend]短信发送phone: {}, templateId: {}, params: {}", phone, templateId, args);
        try {
            sendMessageUtil.sendSms(phone, templateId, args);
        } catch (Exception e) {
            log.warn("[smsSend]短信模板内容为空phone: {}, templateId: {}, params: {}", phone, templateId, args, e);
        }
    }

    /**
     * 异步短信发送方法
     * SMSUtil.asyncSendSMS()
     */
    public static void asyncSendSMS(final String phone, final Template tmpl, final Object... args) {
        ThreadPoolUtil.execute(() -> SMSUtil.sendSMS(phone, tmpl, args));
    }
}
